package com.elinkway.infinitemovies.utils;

import android.util.Log;

import com.elinkway.infinitemovies.application.MoviesApplication;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Author: liuxiaojiang(liuxiaojiang@lvideo.com)
 * Date: 2016-03-31
 * Time: 15:31
 * Description: 用于日志收集
 */
public class LogCat {

    public static final String LOG_FILE_NAME = "YSDQ_LOG.txt";

    public static boolean DEBUG = MoviesConstant.DEBUG;

    private int level = 1;

    public static void v(String tag, String msg) {
        if (DEBUG)
            Log.v(tag, msg);
    }

    public static void v(String tag, String msg, Throwable t) {
        if (DEBUG)
            Log.v(tag, msg);
    }

    public static void d(String tag, String msg) {
        if (DEBUG)
            Log.d(tag, msg);
    }

    public static void d(String tag, String msg, Throwable t) {
        if (DEBUG)
            Log.d(tag, msg);
    }

    public static void i(String tag, String msg) {
        if (DEBUG)
            Log.i(tag, msg);
    }

    public static void i(String tag, String msg, Throwable t) {
        if (DEBUG)
            Log.i(tag, msg);
    }

    public static void w(String tag, String msg) {
        if (DEBUG)
            Log.w(tag, msg);
        appendLog(getLocalTime() + " " + "/E/YSDQ_LOG/" + tag + " : " + msg);
    }

    public static void w(String tag, String msg, Throwable t) {
        if (DEBUG)
            Log.w(tag, msg);
        appendLog(getLocalTime() + " " + "/E/YSDQ_LOG/" + tag + " : " + msg);
    }

    public static void e(String tag, String msg) {
        if (DEBUG) {
            Log.e(tag, msg);
        }
        appendLog(getLocalTime() + " " + "/E/YSDQ_LOG/" + tag + " : " + msg);
    }

    public static void e(String tag, String msg, Throwable t) {
        if (DEBUG) {
            Log.e(tag, msg);
        }
        appendLog(getLocalTime() + " " + "/E/YSDQ_LOG/" + tag + " : " + msg);
    }

    public static String getLocalTime() {
        Date date = new Date();//创建一个时间对象，获取到当前的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");//设置时间显示格式
        String str = sdf.format(date);//将当前时间格式化为需要的类型
        return str;
    }

    public static void appendLog(String log) {
        File logFile = new File("/sdcard/", LOG_FILE_NAME);
        if (!logFile.exists()) {
            try {
                logFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
                        MoviesApplication.isSaveLogContinue));
                buf.append(log);
                buf.newLine();
                buf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
